package com.fan.shop.businesslogic;

import org.apache.log4j.Logger;

import com.fan.shop.domain.Brand;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.BrandService;

public class BrandDeleteProcess extends ProcExec<Integer> {

	private static Logger logger = Logger.getLogger(BrandDeleteProcess.class);
	private Brand brand;
	private BrandService brandService;

	public static Logger getLogger() {
		return logger;
	}

	public Brand getBrand() {
		return brand;
	}

	public void setBrand(Brand brand) {
		this.brand = brand;
	}

	public BrandService getBrandService() {
		return brandService;
	}

	public void setBrandService(BrandService brandService) {
		this.brandService = brandService;
	}

	public static void setLogger(Logger logger) {
		BrandDeleteProcess.logger = logger;
	}

	@Override
	public Integer processImpl() {
		if (brand == null) {
			setErrorMessage("Brand is null!");
			return ShopGlobalVariables.FAILED;
		}
		if (!brandService.hasBrand(brand.getName())) {
			setErrorMessage("Brand " + brand.getName() + " doesn't exist!");
			return ShopGlobalVariables.FAILED;
		}
		if(brandService.getHasBrandItemsFlag(brand.getName()))
		{
			setErrorMessage("Brand " + brand.getName() + " is used in items, cannot be deleted!");
			return ShopGlobalVariables.FAILED;
		}
		try {
			brandService.deleteBrand(brand);
		} catch (Exception e) {
			setErrorMessage(e.getMessage());
			e.printStackTrace();
			return ShopGlobalVariables.FAILED;
		}
		return ShopGlobalVariables.SUCCESS;
	}

}
